//
//  GYPersonalInfoSetViewController.m
//  GoodLuck
//
//  Created by apple on 2018/5/10.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

static NSString *cellIdentifier = @"cellIdentifier";
static NSString *setCellIdentifier = @"setCellIdentifier";


#import "GYPersonalInfoSetViewController.h"
#import "GYNetWorkTools.h"
#import "GYSetCell.h"
#import "GYModifyPasswordViewController.h"
#import "GYModfyPhoneViewController.h"

@interface GYPersonalInfoSetViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation GYPersonalInfoSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setupUI];
}

- (void)setupUI {
    self.navigationItem.title = @"个人信息设置";
    self.tableView.sectionFooterHeight = 10;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section < 2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        }
        cell.textLabel.text = indexPath.section == 0 ? @"昵称" : @"手机号";
        cell.detailTextLabel.text = indexPath.section == 0 ? self.member_name : self.member_mobile;
        
        return cell;
    }else {
        GYSetCell *cell = [GYSetCell cellWithTbaleView:tableView];
        cell.titleLable.text = @"登陆密码";
        cell.contentLable.text = @"建议您定期更改密码以保护账户安全";
        cell.setLable.hidden = YES;
        
        return cell;

    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    view.backgroundColor = [UIColor clearColor];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        GYSetCell *cell = [GYSetCell cellWithTbaleView:tableView];
        
        return [cell getHeight];
    }
    return 49;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"您确定修改昵称吗?" message:self.member_name preferredStyle:UIAlertControllerStyleAlert];
        [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"请输入昵称!";
        }];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        UIAlertAction *sub = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //判断修改
            NSString *nickName = [[alertController.textFields firstObject] text];
            if (nickName.length != 0) {
                [GYNetWorkTools requestModfyUserName:nickName success:^(NSDictionary *obj) {
                    //NSLog(@"obj=======%@",obj);
                    int code = [[obj objectForKey:@"code"] intValue];
                    if (code == 200) {
                        [EasyTextView showSuccessText:@"修改昵称成功!"];
                        self.member_name = nickName;
                        [tableView reloadData];
                    }else if (code == 400) {
                        [EasyTextView showText:@"修改昵称失败!"];
                    }
                } error:^(NSError *error) {
                    [EasyTextView showText:@"修改昵称失败!"];
                }];
            }else {
                [EasyTextView showText:@"请输入有效字符!"];
            }
        }];
        
        [alertController addAction:cancel];
        [alertController addAction:sub];
        [self presentViewController:alertController animated:YES completion:nil];
    }else if (indexPath.section == 1) {
        GYModfyPhoneViewController *modfyPhoneVC = [[GYModfyPhoneViewController alloc] initWithNibName:@"GYModfyPhoneViewController" bundle:nil];
        modfyPhoneVC.bindPhone = NO;
        modfyPhoneVC.member_mobile = self.member_mobile;
        [self.navigationController pushViewController:modfyPhoneVC animated:YES];
        
    }else if (indexPath.section == 2) {
        GYModifyPasswordViewController *phoneVC = [[GYModifyPasswordViewController alloc] initWithNibName:@"GYModifyPasswordViewController" bundle:nil];
        phoneVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:phoneVC animated:YES];

    }
}

@end
